import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { terser } from "rollup-plugin-terser";
import html from "@rollup/plugin-html";
import progress from "rollup-plugin-progress";
import fs from "fs";
import myExample from "./src/plugins/rollup-plugin-my-example.js";
import virtualModule from "./src/plugins/rollup-plugin-virtual-module.js";
import logModule from "./src/plugins/rollup-plugin-log-module.js";
import timer from "./src/plugins/rollup-plugin-timer.js";
import copyResource from "./src/plugins/rollup-plugin-copy-resource.js";
import OutputSize from "./src/plugins/rollup-plugin-output-size.js";
import alias from "@rollup/plugin-alias";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const customHtml = fs.readFileSync("src/index.html", "utf-8");

export default {
  input: "src/index.js",
  output: {
    dir: "dist",
    format: "esm",
    chunkFileNames: "[name]-[hash].js",
    manualChunks: (id) => {
      // 将某个文件单独打包
      if (id.includes("date.js")) {
        return "utils/date";
      } else if (id.includes("server.js")) {
        return "utils/server";
      } else if (id.includes("test.js")) {
        return "test";
      }
    },
    minifyInternalExports: true,
  },
  plugins: [
    alias({
      entries: [{ find: "@/utils", replacement: `${__dirname}/src/utils` }],
    }),
    virtualModule(),
    myExample(),
    html({
      template: () => {
        return customHtml;
      },
    }),
    resolve(),
    commonjs(),
    terser(), // 会将注釋全部删除
    progress({
      clearLine: false, // default: true
    }),
    logModule(),
    timer(),
    copyResource({
      targets: [
        { src: "public", dest: "public" },
        { src: "src/assets", dest: "assets" },
      ],
    }),
    OutputSize(),
  ],
};
